package com.dianrui.qiyouriding.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.bean.BlueNearBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BlueNearAdapter extends BaseAdapter {

    List<BlueNearBean> list;
    Context context;
    public OrderClickListener orderClickListener;

    public BlueNearAdapter(Context context, List<BlueNearBean> list) {
        this.list = list;
        this.context = context;
    }

    public void refresh(List<BlueNearBean> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (list== null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

  public   interface OrderClickListener {
        void onOrderClick(String id, String devices);
    }

    public void setOnButtonClickListener(OrderClickListener orderClickListener) {
        this.orderClickListener = orderClickListener;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bike_point_vehicle_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (null != list && list.size()>0) {
            final BlueNearBean vehicleBean = list.get(position);
            holder.number.setText(vehicleBean.number);
            holder.battery.setText( vehicleBean.battery + "%");
            holder.journey.setText( vehicleBean.journey + "km");
            holder.use.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (orderClickListener != null) {
                        orderClickListener.onOrderClick(vehicleBean.vehicle_id,vehicleBean.device);
                    }

                }
            });
        }
        return convertView;
    }

     class ViewHolder {
        @BindView(R.id.number)
        TextView number;
        @BindView(R.id.journey)
        TextView journey;
        @BindView(R.id.battery)
        TextView battery;
         @BindView(R.id.use)
         TextView use;
        @BindView(R.id.look_me)
        LinearLayout clickItemVehicle;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
